<template>
  <BaseDialog
    :title="(!props.item.managerId ? '新增' : '编辑') + '机器人'"
    :handleCancel="handleCancel"
    :dialogVisible="props.dialogVisible"
  >
    <BaseForm
      labelWidth="100px"
      type="rule"
      :handleOk="handleOk"
      :handleCancel="handleCancel"
      :items="formItems"
      v-model:form="ruleForm"
      :rules="rules"
      :refresh="handleSearch"
    >
    </BaseForm>
  </BaseDialog>
</template>

<script setup lang="ts">
import BaseForm from '@/components/base-form.vue'
import BaseDialog from '@/components/base-dialog.vue'
import { basicConfigService } from '@/api/basicConfig'

const props = defineProps({
  dialogVisible: {
    type: Boolean,
  },
  item: {
    type: Object,
  },
  handleSearch: {
    type: Function,
  },
})
const emit = defineEmits(['update:dialogVisible'])

const formItems = ref([
  {
    name: '机器人名称',
    key: 'managerName',
    type: 'input',
  },
  {
    name: '唯一标识',
    key: 'wxRobotId',
    type: 'input',
    disabled: computed(() => !!props.item.managerId),
    placeholder: '请输入机器人唯一标识',
  },
  {
    name: '供电单位',
    key: 'powerId',
    type: 'select-page',
    clearable: true,
    funName: "getByAreaQueryByPower",
    option: {
      name: "powerName",
      value: "powerId",
    }
  },
  {
    name: '备注',
    key: 'remark',
    type: 'textarea',
    maxlength: '300',
    'show-word-limit': true,
  },
  // {
  //   name: '状态',
  //   key: 'isDisable',
  //   type: 'switch',
  //   activeValue: 0,
  //   inactiveValue: 1,
  // },
])
const rules = reactive({
  managerName: [{ required: true, message: '请输入', trigger: 'change' }],
  wxRobotId: [{ required: true, message: '请输入', trigger: 'change' }],
  province: [{ required: true, message: '请选择', trigger: 'change' }],
  powerId: [{ required: true, message: '请选择', trigger: 'change' }],
  // isDisable: [{ required: true, message: '请选择', trigger: 'change' }],
})
const ruleForm = ref({
  managerId: '',
  managerName: '',
  remark: '',
  wxRobotId: '',
  powerId: '',
  powerName:'',
  // isDisable: '',
})
const handleOk = () => {
  const wxRobotId = ruleForm.value.wxRobotId.trim()
  if (props.item.managerId) {
    return basicConfigService.robotUpdate({
      data: { ...ruleForm.value, wxRobotId },
    })
  } else {
    return basicConfigService.robotBatchSave({
      data: [{ ...ruleForm.value, wxRobotId, isDisable: 1 }],
    })
  }
}
const handleCancel = () => {
  emit('update:dialogVisible', false)
}
onMounted(() => {
  if (props.item.managerId) {
    ruleForm.value.managerId = props.item.managerId
    ruleForm.value.managerName = props.item.managerName
    // ruleForm.value.isDisable = props.item.isDisable
    ruleForm.value.remark = props.item.remark
    ruleForm.value.wxRobotId = props.item.wxRobotId
    ruleForm.value.powerId = props.item.powerId
    ruleForm.value.powerName=props.item.powerName
  }
})
</script>

<style lang="scss" scoped></style>
